﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Services;
using Common;
using EntityModel;
using System.Data.Entity;
using MvcPaging;

namespace Heart.Controllers
{
    public class ArticleController : Controller
    {
        #region 公用属性
        private ArticleService articleService = new ArticleService();
        private ArticleTypeService articleTypeService = new ArticleTypeService();
        private int pageSize = ConfigHelper.PageSize();
        #endregion


        public ActionResult Index(int? page)
        {
            int PageIndex = page.HasValue ? (page.Value == 0 ? 0 : (page.Value - 1)) : 0;
            var list = articleService.GetAllList().ToPagedList(PageIndex, pageSize);
            return View(list);
        }

        public ActionResult Create()
        {
            IEnumerable<ArticleType> list = articleTypeService.GetList();
            ViewData["list"] = new SelectList(list, "Id", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult Create(FormCollection model)
        {
            HttpPostedFileBase file = Request.Files["ImgUrl"];
            string imgUrlPath = "";
            string urlFileName = "";
            //存入文件
            if (file.ContentLength > 0)
            {
                imgUrlPath = Server.MapPath("~/upload/article/");
                urlFileName = "/article/" + file.FileName;
                file.SaveAs(imgUrlPath + System.IO.Path.GetFileName(file.FileName));
            }
            Article a = new Article();
            a.ImgUrl = urlFileName;
            a.Title = WebBase.GetForm("Title");
            a.FromUrl = WebBase.GetForm("FromUrl");
            a.MetaDescription = WebBase.GetForm("MetaDescription");
            a.MetaKeywords = WebBase.GetForm("MetaKeywords");
            a.ShortUrl = WebBase.GetForm("ShortUrl");
            a.Status = 1;
            a.Tags = WebBase.GetForm("Tags");
            a.Clicks = WebBase.GetFormInt("Clicks");
            a.Contents = WebBase.GetForm("Contents");
            a.CreateOn = DateTime.Now;
            a.DisplayOrder = WebBase.GetFormInt("DisplayOrder");
            a.ArticleTypeId = WebBase.GetFormInt("ArticleTypeId");
            a.ArticleTypeId = WebBase.GetFormInt("ArticleTypeId");                
            a.DisplayOrder = WebBase.GetFormInt("DisplayOrder");
            articleService.Create(a);
            return RedirectToAction("Index");
        }




    }
}
